#include"GameDraw.h"

int plusdir = 0;
int rand_dir = 0;

void setPox(int x, int y)
{
	COORD pox = { x,y };
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOut, pox);
}

void setColor(unsigned short color)
{
	HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hCon, color);
}

void outTom(int x, int y) {
	setPox(x, y);
	printf("+");
	//��
}

void outJerry(int x, int y) {
	setPox(x, y);
	printf("@");
}

void outCake(int x, int y) {
	setPox(x, y);
	printf("c");
}

void outDrink(int x, int y) {
	setPox(x, y);
	printf("d");
}

void outSpace(int x, int y) {
	setPox(x, y);
	printf(" ");
}

void showMaze(int** map) {
	for (int i = 0; i < L; i++) {
		for (int j = 0; j < L; j++) {
			if (map[i][j] == ROUTE) {
				printf(" ");
			}
			else {
				printf("*");
				//��
			}
		}
		printf("\n");
	}
}

void generateCake(int** map) {


	while (1) {
		srand((unsigned)time(NULL));
		srand(rand() + (unsigned int)time(NULL));
		int x = rand() % L;
		int y = rand() % L;

		if (map[x][y] == ROUTE) {
			map[x][y] = 2;	//CAKE
			outCake(y, x);

		}


		Sleep(10000);
	}


}

void generateDrink(int** map) {

	while (1) {
		srand((unsigned)time(NULL));
		srand(rand() + (unsigned int)time(NULL));
		int x = rand() % L;
		int y = rand() % L;

		if (map[x][y] == ROUTE) {
			map[x][y] = 3;		//DRINK
		}

		outDrink(y, x);

		Sleep(10000);
	}
}

Pos makeDrink(int** map) {
	Pos drink;
	drink.x = rand() % L;
	drink.y = rand() % L;
	while (map[drink.x][drink.y] == WALL)
	{
		drink.x = rand() % L;
		drink.y = rand() % L;
	}

	map[drink.x][drink.y] = 3;		//DRINK

	outDrink(drink.y, drink.x);

	return drink;
}

Pos makeCake(int** map) {
	Pos cake;
	cake.x = rand() % L;
	cake.y = rand() % L;
	while (map[cake.x][cake.y] == WALL)
	{
		cake.x = rand() % L;
		cake.y = rand() % L;
	}

	map[cake.x][cake.y] = 2;		//CAKE

	outDrink(cake.y, cake.x);

	return cake;
}

void saveAndShowCake(int** map, Queue* cakes, int num) {
	srand((unsigned)time(NULL));
	for (int i = 0; i < num; i++) {
		Pos cake = makeDrink(map);
		cakes->enQueue(cake);
	}
}

void saveAndShowDrink(int** map, Queue* drinks, int num) {
	srand((unsigned)time(NULL));
	for (int i = 0; i < num; i++) {
		Pos drink = makeDrink(map);
		drinks->enQueue(drink);
	}
}

int plusDirection() {
	srand((unsigned)time(NULL));
	if (plusdir == 0) {
		rand_dir = rand() % 4;
	}
	if (plusdir == rand_dir) {
		srand((unsigned)time(NULL));
		plusdir = rand() % 4;
	}
	plusdir = (plusdir + 1) % 4;
	return plusdir;
}

void goTom(int** map, Tom* tom, Queue path) {
	while (!path.isEmpty()) {
		int x = tom->getX();
		int y = tom->getY();
		Pos p = path.deQueue();
		tom->setPosition(p.x, p.y);
		outTom(p.y, p.x);
		outSpace(y, x);
		Sleep(100);
	}
}

void goJerry(int** map, Jerry* jerry, Queue path) {
	while (!path.isEmpty()) {
		int x = jerry->getX();
		int y = jerry->getY();
		Pos p = path.deQueue();
		jerry->setPosition(p.x, p.y);
		outJerry(p.y, p.x);
		outSpace(y, x);
		Sleep(100);
	}
}

void setJerryPos(int** map, Jerry* jerry, Tom* tom) {
	int tom_x = tom->getX();
	int tom_y = tom->getY();
	double pos;
	pos = sqrt((jerry->getX() - tom_x) * (jerry->getX() - tom_x) + (jerry->getY() - tom_y) * (jerry->getY() - tom_y));
	srand((unsigned)time(NULL));
	srand(rand() + (unsigned int)time(NULL));
	int x = rand() % L;
	int y = rand() % L;
	while (map[x][y] == WALL && pos < 10) {
		x = rand() % L;
		y = rand() % L;
	}
	jerry->setPosition(x, y);

}